import com.troyworks.particleengine.ParticleSim;
import com.troyworks.framework.ui.layout.LayoutHelper;
import com.troyworks.geom.d2.Rect2D;
import com.troyworks.framework.ui.layout.Bounds2;
import com.troyworks.framework.ui.DraggableClip;
/**
 * @author Troy Gardner
 */
class com.troyworks.particleengine.FieldEffect extends ParticleSim {
	public function FieldEffect() {
		numOfParticles =20;
		linkageID = "Clip";
	}
	public function onLoad() : Void{
		bounds = Rect2D.createRectFromClipBounds(this, this);
		layoutMan = new LayoutHelper(LayoutHelper.LINEAR, bounds);
		createParticles();
	}
	public function inflate(partA : MovieClip) : Void{
		var b : Bounds2 = new Bounds2(partA.getBounds(this));
		var dx : Number = _xmouse - b.xMid;
		var dy : Number = _ymouse - b.yMid;;
		var distSQ : Number = dx*dx + dy*dy;
		var dist : Number = Math.sqrt(distSQ);
		var minScale:Number = 20;
		var maxScale:Number = 1000;
		var force : Number = Math.max(1/ Math.max(dist,1) * maxScale, minScale);
		var minDist:Number = 300;
		if(dist < 150){
		//	force = (Math.cos((minDist- dist))+1)*100;
		}
		partA._xscale = partA._yscale = force;
	}
	public function inflate2(partA:MovieClip):Void{
		var icon_min:Number = 32;
		var icon_size:Number = 100;
		var icon_max:Number = 128;
		var icon_spacing:Number = 2;
		var span:Number=(icon_min - 16) * (240 - 60) / (96 - 16) + 60;
		var ratio:Number =2;
		var dim:Number;
		var scale:Number = 1;
		var amplitude:Number = 2 * (icon_max - icon_min +icon_spacing);
		
			var dx:Number = partA._x - _xmouse;
			dx = Math.min(Math.max(dx, -span), span);
			dim = icon_min + (icon_max - icon_min) * Math.cos(dx * ratio) * (Math.abs(dx) > span ? 0 : 1) * scale;
		//	}
		//	p.x = p.x + scale * amplitude * Math.sin(dx * ratio);
			partA._x = partA.x + scale * amplitude * Math.sin(dx * ratio);
			partA._xscale = partA._yscale =  100 * dim / icon_size;
			
		//	_mc._x = p.x;
		//	_mc._y = p.y;
		//	_mc._xscale = _mc._yscale =  100 * dim / icon_size;
	}
	public function lenticularLens(partA : MovieClip) : Void{
		var dx : Number = _xmouse - partA._x;
		var adx : Number = Math.abs(dx);
		var dy : Number = _ymouse - partA._y;
		var ady : Number = Math.abs(dy);
		
		if(adx <50 ){
			var force : Number = 50 -(adx) + 20 ;
			partA._xscale = partA._yscale = force;
		}else if(ady < 50){
			var force : Number = 50- (ady)+ 20 ;
			partA._xscale = partA._yscale = force;
		}
	}
	public function onEnterFrame():Void{
	
		var cx : Number = 0;
		var midIdx:Number = 0;
		var midMc:MovieClip;
		for (var i : Number = 0; i < clips.length; i++) {
			var _mc : DraggableClip = DraggableClip(clips[i]);
			inflate2(_mc);
		}	
	}
	/*********** an attempt at layout left and right*****/
	public function onEnterFrame3() : Void{
		var cx : Number = 0;
		var midIdx:Number = 0;
		var midMc:MovieClip;
		for (var i : Number = 0; i < clips.length; i++) {
			var _mc : DraggableClip = DraggableClip(clips[i]);
				var b : Bounds2 = new Bounds2(_mc.getBounds(this));
				if( b.contains(_xmouse,_ymouse)){
					midIdx = i;
					inflate(_mc);
					midMc = _mc;
				//	break;
					
				}
				_mc._x = cx;
				cx += _mc._width;
				_mc._y = 0;
		}
		var l:Number = midIdx;
		var cx:Number = midMc._x;
		while(l--){
			var _mc : DraggableClip = DraggableClip(clips[l]);
			inflate(_mc);
			cx -= _mc._width;
			_mc._x = cx;
		}
		cx = midMc._x + midMc._width;
		for (var i : Number = midIdx; i < clips.length; i++) {
				var _mc : DraggableClip = DraggableClip(clips[i]);
			_mc._x = cx;
				
			_mc._y = 20;	
			inflate(_mc);
			cx += _mc._width;
		}

	}
	public function onEnterFrame2() : Void{
		var cx : Number = 0;
		for (var i : Number = 0; i < clips.length; i++) {
			var _mc : DraggableClip = DraggableClip(clips[i]);
		//	trace("checking " + _mc._name + " xm: " + _xmouse + " ym: " + _ymouse + " x: " + _mc._x  + " width " +(_mc._x+_mc._width)+ " y " +_mc._y+ " height " + (_mc._y+_mc._height));
			if(!_mc.isBeingDragged){
				_mc._x = cx;
				inflate(_mc);
				cx += _mc._width;
			}else{
				var b : Bounds2 = new Bounds2(_mc.getBounds(this));
				var dx : Number = 0;// b.xMid;
				var dy : Number =  _mc._y;//b.yMin;
				var distSQ : Number = dx*dx + dy*dy;
				var dist : Number = Math.sqrt(distSQ);
				trace("dist " + dist);
				if(dist <_mc._width + 40){
				//200/
				//when dist == 1, width = 200;
				//when disth == 200, width = 0;
					var wid = _mc._width-dist + 40;
				//		trace("splicing" + wid);
					cx +=  wid ;
				}
					//	_mc._x = cx;
				//inflate(_mc);
			//	cx += _mc._width;
		//	lenticularLens(_mc);
			}
		}
	}
}